﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;

namespace DoubleClick
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();

            if (Program.Default.StartupMode == StartupModeEnum.NotSet)
                Environment.Exit(1);

            ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
            ServiceInstaller serviceInstaller = new ServiceInstaller();

            serviceProcessInstaller.Account = ServiceAccount.LocalSystem;

            serviceInstaller.ServiceName = String.Format("dblClick_{0}", Program.Default.ServiceName);
            serviceInstaller.Description = String.Format("Servizio DoubleClick per {0}", Program.Default.ServiceName);

            Installers.AddRange(
                new Installer[] 
                { 
                    serviceProcessInstaller,
                    serviceInstaller
                }
            );
        }

        public override void Install(IDictionary stateSaver)
        {
            if (Program.Default != null)
                base.Context.Parameters["assemblypath"] =
                    string.Format("{0}\" service \"{1}", 
                    base.Context.Parameters["assemblypath"],
                    Program.Default.CommandToRun);
            base.Install(stateSaver);
        }
    }
}
